#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Mikan Key Value Store Package

Mikan Python Web Framework
"""

__author__ = "K.Sonohara"
__status__ = "develop"
__version__ = "0.0.0_36"
__date__    = "2014/01/05"
__license__ = 'LGPL'

# ------------------------------------------------------------
## Import Section
# ------------------------------------------------------------
# Python
import traceback
import datetime

# ESiPC
from esipc import ESiPCProperty

# Mikan
from mikan.db import AbstractMikanItemDatabase

# ------------------------------------------------------------
## Variable Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Function Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Class Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Mikan DB抽象 クラス
#
class AbstractMikanKVS(AbstractMikanItemDatabase):

	def set_connector(self, value):
		self.FConnector = value

	def get_connector(self):
		return self.FConnector

	def __init__(self, request):
		# super
		super(AbstractMikanKVS, self).__init__(request)

		# variable
		self.FConnector   = None

	def connect(self):
		pass

	def close(self):
		if (self.connector != None):
			try:
				self.connector.rollback()
			except:
				pass
			try:
				self.connector.close()
			except:
				pass
		self.FConnector   = None

	def commit(self):
		pass

	def rollback(self):
		pass

	def get_version(self):
		return self.get_kvs_version()

	def get_name(self):
		return self.get_kvs_name()

	def get_kvs_version(self):
		return ""

	def get_kvs_name(self):
		return ""

	# ----------------------------------------------------
	## Property
	connector = ESiPCProperty(get_connector, set_connector, None)
	kvsversion = ESiPCProperty(get_kvs_version, None, None)
	kvsname = ESiPCProperty(get_kvs_name, None, None)

# ------------------------------------------------------------
## Mikan DB抽象 クラス
#
def new_mikan_kvs(request, dbtype="mongodb"):
		t = None

		if (dbtype == "mongodb"):
			t = "MikanMongoDBKVS"

		try:
			module = "mikan.kvs." + dbtype
			fromlist = [module]
			module = __import__(module, fromlist=fromlist)

			c = getattr(module, t)
			return c(request)
		except:
			if request:
				try:
					request.logger.error(t)
					request.logger.error(module)
					request.logger.error([module])
				except:
					pass
				request.logger.error(traceback.format_exc())
			else:
				print traceback.format_exc()

		return None

# ------------------------------------------------------------
## Main Section
# ------------------------------------------------------------
if __name__ == '__main__':
	pass

# ------------------------------------------------------------
